package com.young.shop_admin.dao.impl;

import com.young.shop_admin.dao.OrderDao;
import com.young.shop_admin.pojo.entity.Order;
import com.young.shop_admin.pojo.qo.PaymentQO;
import com.young.shop_admin.pojo.vo.ManagementCenterDataVO;
import com.young.shop_admin.pojo.vo.OrderGoodsDetailVO;
import com.young.shop_admin.utils.DBUtils;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    //创建自动化管理工具
    private static QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

    //开启驼峰映射
    private static BeanProcessor bean = new GenerousBeanProcessor();
    private static RowProcessor processor = new BasicRowProcessor(bean);

    //时间格式
    private static SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<Order> getAllOrders() {
        //执行的SQL语句
        String sql = "SELECT * FROM a_orders";
        //定义返回形式
        BeanListHandler<Order> orderBeanListHandler = new BeanListHandler<>(Order.class, processor);
        //执行sql
        try {
            return queryRunner.query(sql, orderBeanListHandler);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateOrderById(int id, String status) {
        //执行的sql语句
        String sql = "UPDATE a_orders SET `status` = ?,delivery_date = ? WHERE ID = ? ";
        //获取当前时间
        String dateFormat = sd.format(new Date());
        //参数封装
        Object[] params = {status, dateFormat, id};
        //执行sql
        try {
            return queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateOrderByIdForCancel(int id, String status) {
        //执行的sql语句
        String sql = "UPDATE a_orders SET `status` = ?,cancel_date = ? WHERE ID = ? ";
        //获取当前时间
        String dateFormat = sd.format(new Date());
        //参数封装
        Object[] params = {status, dateFormat, id};
        //执行sql
        try {
            return queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Order queryOrderById(int id) {
        //执行的sql语句
        String sql = "SELECT * FROM a_orders WHERE id = ?";
        //定义返回的类型
        BeanHandler<Order> orderListBeanHandler = new BeanHandler<>(Order.class, processor);
        //执行sql
        try {
            return queryRunner.query(sql, orderListBeanHandler, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public List<OrderGoodsDetailVO> queryOrderGoodsDetailById(int id) {
        //执行的sql语句
        String sql = "SELECT g.name, g.price, g.image_url,o.number FROM t_goods g LEFT JOIN t_order_list o ON g.id = o.gid LEFT JOIN a_orders a ON o.aid = a.id WHERE a.id = ?";
        //定义返回的类型
        BeanListHandler<OrderGoodsDetailVO> orderGoodsDetailBeanHandler = new BeanListHandler<>(OrderGoodsDetailVO.class, processor);
        //执行sql
        try {
            return queryRunner.query(sql, orderGoodsDetailBeanHandler, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Order> getOrderByDate(String beforeDate, String afterDate) {
        //执行的sql语句
        String sql = "select * from a_orders where create_date between ? and ? ORDER BY id DESC";
        //定义返回类型
        BeanListHandler<Order> orderBeanListHandler = new BeanListHandler<>(Order.class, processor);
        //封装参数
        Object[] params = {beforeDate, afterDate};
        //执行sql
        try {
            return queryRunner.query(sql, orderBeanListHandler, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public PaymentQO getAddPayment(String beforeDate, String nowDate) {
        //执行的sql语句
        String sql = "select SUM(amount) AS payment from a_orders WHERE create_date BETWEEN ? AND ?";
        //定义返回类型
        BeanHandler<PaymentQO> paymentBeanHandler = new BeanHandler<>(PaymentQO.class, processor);
        //参数封装
        Object[] params = {beforeDate, nowDate};
        try {
            return queryRunner.query(sql, paymentBeanHandler, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }
}
